/// Setting up the ANES File for presidential coalitions  

use "/Users/joshuazingher/Desktop/Prospectus /anes_cdfdta/anes coalitions .dta"

sort year

rename VCF0704 presvote

rename VCF0703 voted

rename ADem Adem

gen ADem = Adem/(ARep+Adem)



/// Generating total Education respondants by year

by year: egen educationresp = count(Education) if Education == 4

/// Generating total number of Education voters by year


by year: egen Educationvoted = count(voted) if Education==4 & voted==3

/// Generating total number of Latnio non-voters by year

by year: egen EducationNvoted = count(voted) if voted==0 & Education==4 | voted==1 & Education==4 | voted==2 & Education==4

/// Generating total number of non-Education voters by year

by year: egen NEducationvoted = count(voted) if Education~=4 & voted==3

/// Generating total number of non-Education non-voters by year

by year: egen NEducationNvoted = count(voted) if Education~=4 & voted==0 | Education~=4 & voted==1 | Education~=4 & voted==2

/// filling in the blanks

by year: egen educationvoted = max(Educationvoted)
by year: egen educationNvoted = max(EducationNvoted)
by year: egen Neducationvoted = max(NEducationvoted)
by year: egen NeducationNvoted = max(NEducationNvoted)




/// Generating % Education TO with Mosteller Correction

gen N = 1
gen a = [educationvoted * NeducationNvoted] / [educationNvoted * Neducationvoted]


///Generating filler vars

gen aminone =(a-1)
gen ACollegeplusTO = (ACollege+aTO)
gen aEducationTO=(a*ACollege*aTO)

gen CEducationTO = 1/ (2*(aminone)) * (N+(aminone*ACollegeplusTO) - sqrt((N+aminone*ACollegeplusTO)*(N+aminone*ACollegeplusTO) - (4*(aminone*aEducationTO))))

///Adjusting TO for the formula

replace CEducationTO = CEducationTO/ACollege



/// Moving to Partisanship - Generating Education Democratic Vote total by year

by year: egen Educationdem = count(Education) if presvote==1 & Education==4

/// Generating Education Non-Democratic Vote by Year

by year: egen EducationNdem = count(Education) if presvote==2 & Education==4

// Generating non-Education Democratic Vote total by year

by year: egen NEducationdem = count(Education) if presvote==1 & Education~=4

/// Generating non-Education non-Democratic vote total by year

by year: egen NEducationNdem = count(Education) if presvote==2 & Education~=4

/// filling in the blanks

by year: egen educationDem = max(Educationdem)
by year: egen educationNDem = max(EducationNdem)
by year: egen NeducationDem = max(NEducationdem)
by year: egen NeducationNDem = max(NEducationNdem)

/// Generation % Education Democrat with Mosteller Correction

gen A = (educationDem*NeducationNDem)/(educationNDem*NeducationDem)
gen Aminone =(A-1)
gen ACollegeplusDem = (ACollege+ADem)
gen ACollegeDem=(A*ACollege*ADem)
gen CEducationDem = 1/(2*(Aminone)) * (N+(Aminone*ACollegeplusDem) - sqrt((N+Aminone*ACollegeplusDem)*(N+Aminone*ACollegeplusDem) - (4*(Aminone*ACollegeDem))))

replace CEducationDem = CEducationDem/ACollege


/// Generating number of ANES respondents by year

by year: egen totalresp = count (VCF0006a)


/// Coalition Contribution

gen educationcoalcontrib = (CEducationTO* CEducationDem *ACollege)/(aTO*ADem)



